package com.danger.Presenter;

import android.content.Context;

import com.danger.Model.action.UserInfoModel;
import com.danger.Model.action.UserInfoModelImpl;
import com.danger.Model.listener.OnCommonListener;
import com.danger.View.CommonView;

/**
 * Created by danger on 2016/4/25.
 */
public class UserInfoPresenterImpl implements UserInfoPresenter {

    private Context context;
    private UserInfoModel model;
    private CommonView view;

    public UserInfoPresenterImpl(Context context, CommonView view) {
        this.context = context;
        this.view = view;
        model = new UserInfoModelImpl(context);
    }

    @Override
    public void updateUsername(String username, String id) {
        view.showDialog();
        model.modifyUsername(username, id, new OnCommonListener() {
            @Override
            public void onSuccess(String msg) {
                view.hideDialog();
                view.showSuccess(msg, null);
                view.back();
            }

            @Override
            public void onError(String error) {
                view.hideDialog();
                view.showError(error);
            }
        });
    }

    @Override
    public void updatePassword(String password, String confirm_password, String id) {
        view.showDialog();
        model.modifyPassword(password, confirm_password, id, new OnCommonListener() {
            @Override
            public void onSuccess(String msg) {
                view.hideDialog();
                view.showSuccess(msg, null);
                view.back();
            }

            @Override
            public void onError(String error) {
                view.hideDialog();
                view.showError(error);
            }
        });
    }

    @Override
    public void updateEmail(String email, String id) {
        view.showDialog();
        model.modifyEmail(email, id, new OnCommonListener() {
            @Override
            public void onSuccess(String msg) {
                view.hideDialog();
                view.showSuccess(msg, null);
                view.back();
            }

            @Override
            public void onError(String error) {
                view.hideDialog();
                view.showError(error);
            }
        });
    }

    @Override
    public void updatePhone(String phone, String id) {
        view.showDialog();
        model.modifyPhone(phone, id, new OnCommonListener() {
            @Override
            public void onSuccess(String msg) {
                view.hideDialog();
                view.showSuccess(msg, null);
                view.back();
            }

            @Override
            public void onError(String error) {
                view.hideDialog();
                view.showError(error);
            }
        });
    }

    @Override
    public void updatePhoto(String path, final String id) {
        view.showDialog();
        model.upload(path, new OnCommonListener() {
            @Override
            public void onSuccess(String msg) {
                model.modifyPhoto(msg, id, new OnCommonListener() {
                    @Override
                    public void onSuccess(String s) {
                        view.hideDialog();
                        view.showSuccess(s, null);
                        view.back();
                    }

                    @Override
                    public void onError(String error) {
                        view.hideDialog();
                        view.showError(error);
                    }
                });
            }

            @Override
            public void onError(String error) {
                view.hideDialog();
                view.showError(error);
            }
        });
    }
}
